#include "lvgl.h"
#include "rtthread.h"
#include "rtdbg.h"
#include "lv_demo.h"
#include "applications/tft_lcd/pic.h"
#include "applications/flash_25q64/25q64.h"

#define LV_DEMO_PARAMETER       RT_NULL
#ifndef LV_DEMO_THREAD_STACK_SIZE
#define LV_DEMO_THREAD_STACK_SIZE      1024
#endif
#ifndef LV_DEMO_PRIORITY
#define LV_DEMO_PRIORITY        8   
#endif     


static void lv_arc_fan_speed_disp_cb(lv_event_t * e);
static void img_click_event_cb(lv_event_t * e);
static void back_btn_event_cb(lv_event_t * e);
static void encoder_switch_event_cb(lv_event_t *e);
static void create_home_screen(void);
static void create_sleeping_screen(void);
void update_temp_disp_number(int num);
extern rt_uint16_t temp_adc_get_value(void);
extern rt_uint16_t temp_set_pid_period(rt_int16_t current_temp, rt_int16_t temp_set_target);
extern void fan_set_period(rt_int8_t pulse_temp);
extern rt_uint16_t temp_adc_get_value(void);
extern rt_uint8_t fan_switch_get_pin(void);


static lv_group_t *g;
static lv_group_t *g_setting;
static lv_obj_t * home_screen;
// 新界面的屏幕对象
static lv_obj_t * settings_screen;
static lv_obj_t * sleeping_screen;
static lv_obj_t * spinbox ;
static lv_obj_t * temp_disp_lable;
static lv_obj_t * chart;                    // 图表对象
static lv_chart_series_t *ser1;             // 数据系列1
static lv_chart_series_t *ser2;             // 数据系列2
static lv_obj_t * arc;
static lv_obj_t * arc_label;

static rt_uint32_t temperature_average = 0;

typedef struct {
    const char* name;
    lv_obj_t *switch_id;
} switch_info_t;
switch_info_t switch_ptrs[] = {
    {"encoder_switch", NULL}
};


// 定时器回调函数 - 定期添加新数据
static void add_data(void)
{
    static rt_uint8_t close_fan_cnt = 0; 
    static rt_uint16_t temp_set_value = 0;
    static rt_uint8_t value;
    static bool sleep_flag = false;

    if (spinbox == NULL || arc == NULL) return;
    if (temperature_average <= 0) return;
    // 为第一个数据系列添加随机值
    lv_chart_set_next_value(chart, ser1, (rt_uint16_t) temperature_average);

    // 将ADC值存入buf指向的内存
    update_temp_disp_number((rt_uint16_t) temperature_average);


    if (temp_set_value != lv_spinbox_get_value(spinbox)) {
        temp_set_value = lv_spinbox_get_value(spinbox);
        memcpy(flash_dat[0].buf, &temp_set_value, sizeof(rt_uint16_t));
        // LOG_D("flash_dat[0].buf:%d", *(rt_uint16_t*)flash_dat[0].buf);
        flash_dat[0].dirty_flag = RT_TRUE;
    }
        
    if(value != lv_arc_get_value(arc)) {
        value = lv_arc_get_value(arc);
        /*刷新风速设置*/
        memcpy(flash_dat[2].buf, &value, sizeof(rt_uint8_t));
        rt_kprintf("value:%d\n", value);
        flash_dat[2].dirty_flag = RT_TRUE;
    }

    if(fan_switch_get_pin()) {
        close_fan_cnt = 0;
        temp_set_pid_period(temperature_average, temp_set_value);  
        /*设置风速*/
        fan_set_period(value);
        if (sleep_flag == true) {
            sleep_flag = false;
            lv_scr_load_anim(home_screen, LV_SCR_LOAD_ANIM_MOVE_RIGHT, 300, 0, false);
        }
        // if(sleeping_screen != NULL) {
        //     lv_obj_del(sleeping_screen);
        // }
        
    }
    else {
        if(++close_fan_cnt >= 2)
        {
            temp_set_pid_period(0, 0);
            fan_set_period(0);
            if (sleep_flag == false) {
                sleep_flag = true;
                if(sleeping_screen == NULL) {
                    create_sleeping_screen();
                }
                lv_scr_load_anim(sleeping_screen, LV_SCR_LOAD_ANIM_MOVE_LEFT, 300, 0, false);
            }
        }
    }
        
}

static void create_home_screen(void)
{
    home_screen = lv_obj_create(NULL);
#if 1
    /* 创建图表对象 */
    chart = lv_chart_create(home_screen);           // 在活动屏幕上创建图表
    // lv_obj_clear_flag(chart, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_size(chart, 280, 120);               // 设置图表尺寸
    lv_obj_align(chart, LV_ALIGN_BOTTOM_MID, 20, 0);                           // 居中显示
    lv_chart_set_type(chart, LV_CHART_TYPE_LINE);   // 设置为折线图类型（显示线条和点）

    /* 设置分割线数量：6条垂直分割线，7条水平分割线 */
    lv_chart_set_div_line_count(chart, 6, 5);

    // 使用小字体以显示更多标签
    lv_obj_set_style_text_font(chart, &lv_font_montserrat_16, 0);
    lv_obj_set_style_text_color(chart, lv_color_hex(COLOR_WHITE), LV_PART_TICKS);
    lv_obj_set_style_line_color(chart, lv_color_hex(COLOR_FOREST_GREEN), LV_PART_MAIN);
    lv_obj_set_style_line_dash_width(chart, 5, LV_PART_MAIN);    // 虚线段的长度
    lv_obj_set_style_line_dash_gap(chart, 3, LV_PART_MAIN);       // 虚线间隙

    // 设置Y轴范围
    lv_chart_set_range(chart, LV_CHART_AXIS_PRIMARY_Y, 0, 600);
    lv_chart_set_axis_tick(chart, LV_CHART_AXIS_PRIMARY_Y, 5, 0, 7, 100, true, 40);
    
    /* 将旧数据向左移动，新数据添加到右侧 */
    lv_chart_set_update_mode(chart, LV_CHART_UPDATE_MODE_SHIFT);

    /* 添加两个数据系列 */
    ser1 = lv_chart_add_series(chart, lv_palette_main(LV_PALETTE_RED), LV_CHART_AXIS_PRIMARY_Y);    // 红色系列，主Y轴

    /* 初始化数据：为两个系列添加初始随机值 */
    uint32_t i;
    for(i = 0; i < 10; i++) {
        lv_chart_set_next_value(chart, ser1, 0);
    }
        
#endif
    rt_thread_mdelay(100);
    lv_refr_now(NULL);
#if 1   /*创建风速arc*************************************************************** */
    // 创建一个容器来包装圆弧和标签
    lv_obj_t * arc_container = lv_obj_create(home_screen);
    lv_obj_remove_style_all(arc_container);  // 移除容器默认样式
    lv_obj_set_size(arc_container, 120, 120);
    lv_obj_align(arc_container, LV_ALIGN_TOP_MID, -50, 0);

    arc = lv_arc_create(arc_container);
    lv_obj_set_size(arc, 100, 100);
    lv_arc_set_rotation(arc, 135);
    lv_arc_set_bg_angles(arc, 0, 270);
    lv_obj_center(arc);
    lv_obj_set_style_arc_width(arc, 8, LV_PART_MAIN);

    // 外边框焦点指示
    lv_obj_set_style_radius(arc, 10, LV_STATE_FOCUSED);  // 圆角
    lv_obj_set_style_outline_width(arc, 2, LV_STATE_FOCUSED);
    lv_obj_set_style_outline_color(arc, lv_color_hex(COLOR_LIME_GREEN), LV_STATE_FOCUSED);
    lv_obj_set_style_outline_pad(arc, 2, LV_STATE_FOCUSED);

    // 设置范围为 1-5
    lv_arc_set_range(arc, 1, 5);
    lv_arc_set_value(arc, *(rt_uint8_t *)flash_dat[2].buf);  // 设置初始值为 1

    // 添加编码器事件支持
    lv_group_add_obj(g, arc);
    // // 创建文本标签 "FAN"
    // lv_obj_t * arc_label_text = lv_label_create(arc_container);
    // lv_label_set_text(arc_label_text, "FAN");
    // lv_obj_set_style_text_color(arc_label_text, lv_color_hex(COLOR_WHITE), 0);  // 修复：使用正确的对象
    // lv_obj_align(arc_label_text, LV_ALIGN_CENTER, 0, -10);  // 文本在上方

    // 创建数值标签
    arc_label = lv_label_create(arc);
    char buf[4];
    lv_snprintf(buf, sizeof(buf), "%d", lv_arc_get_value(arc));
    lv_label_set_text(arc_label, buf);
    lv_obj_set_style_text_color(arc_label, lv_color_hex(COLOR_YELLOW), 0);  // 修复：使用正确的对象
    lv_obj_set_style_text_font(arc_label, &lv_font_montserrat_72, 0);
    // lv_obj_align_to(arc_label, arc_label_text, LV_ALIGN_OUT_BOTTOM_MID, 0, 5);  // 数值在文本下方
    lv_obj_center(arc_label);

    // 添加事件回调
    lv_obj_add_event_cb(arc, lv_arc_fan_speed_disp_cb, LV_EVENT_VALUE_CHANGED, arc_label);
#endif
    rt_thread_mdelay(100);
    lv_refr_now(NULL);
#if 1    /**************************************************************************** */
    // 创建标签显示温度数字(大温度标签)
    temp_disp_lable = lv_label_create(home_screen);
    // 先设置文本内容，再设置样式
    lv_label_set_text(temp_disp_lable, "888 °C");
    // 设置大字体样式
    lv_obj_set_style_text_font(temp_disp_lable, &lv_font_montserrat_72, 0);  // 72像素字体
    lv_obj_set_style_text_align(temp_disp_lable, LV_TEXT_ALIGN_CENTER, 0);
    lv_obj_set_style_text_color(temp_disp_lable, lv_color_hex(COLOR_RED), 0);
    // 对齐到右上角
    lv_obj_align(temp_disp_lable, LV_ALIGN_TOP_RIGHT, 5, 5);

    /*创建set temp温度数字*************************************************************** */
    rt_thread_mdelay(100);
    spinbox = lv_spinbox_create(home_screen);
    lv_spinbox_set_range(spinbox, 80, 500);
    lv_spinbox_set_value(spinbox, *(rt_uint16_t*)flash_dat[0].buf);     // 加载预设的值
    lv_spinbox_set_digit_format(spinbox, 3, 0);
    lv_obj_set_width(spinbox, 72);
    lv_spinbox_step_prev(spinbox);
    lv_obj_set_style_bg_color(spinbox, lv_color_hex(COLOR_DARK_GRAY), 0);
    lv_obj_align_to(spinbox, temp_disp_lable, LV_ALIGN_OUT_BOTTOM_RIGHT, -10, 10);
    
    // 外边框焦点指示
    lv_obj_set_style_outline_width(spinbox, 2, LV_STATE_FOCUSED);
    lv_obj_set_style_outline_color(spinbox, lv_color_hex(COLOR_LIME_GREEN), LV_STATE_FOCUSED);
    lv_obj_set_style_outline_pad(spinbox, 3, LV_STATE_FOCUSED);
    // 文本样式
    lv_obj_set_style_text_font(spinbox, &lv_font_montserrat_24, 0);
    lv_obj_set_style_text_color(spinbox, lv_color_hex(COLOR_YELLOW), 0);
    lv_obj_set_style_text_align(spinbox, LV_TEXT_ALIGN_CENTER, 0);

    lv_obj_t * temp_set_lable_to_set = lv_label_create(home_screen);
    // 先设置文本内容，再设置样式
    lv_label_set_text(temp_set_lable_to_set, "SET");
    lv_obj_set_style_text_font(temp_set_lable_to_set, &lv_font_montserrat_24, 0);  // 24像素字体
    lv_obj_set_style_text_align(temp_set_lable_to_set, LV_TEXT_ALIGN_CENTER, 0);
    lv_obj_set_style_text_color(temp_set_lable_to_set, lv_color_hex(COLOR_WHITE), 0);
    // 对齐到左边
    lv_obj_align_to(temp_set_lable_to_set, spinbox, LV_ALIGN_OUT_LEFT_MID, -10, 0);
    // 添加编码器事件支持
    lv_group_add_obj(g, spinbox);
#endif
    rt_thread_mdelay(100);
    lv_refr_now(NULL);
#if 1
    LV_IMG_DECLARE(img_cogwheel_argb);

    static lv_style_prop_t tr_prop[] = {
    LV_STYLE_TRANSFORM_WIDTH, 
    LV_STYLE_IMG_RECOLOR_OPA, 
    0  // 数组结束标记
    };

    /* 定义过渡动画描述符 */
    static lv_style_transition_dsc_t tr;
    // 初始化过渡动画：属性数组、线性动画路径、200ms持续时间、0延迟、无用户数据
    lv_style_transition_dsc_init(&tr, tr_prop, lv_anim_path_linear, 200, 0, NULL);

    /* 创建默认状态下的样式 */
    static lv_style_t style_def;
    lv_style_init(&style_def);  // 初始化样式
    lv_style_set_text_color(&style_def, lv_color_white());  // 设置文本颜色为白色
    lv_style_set_transition(&style_def, &tr);  // 应用过渡动画效果

    /* 创建按下状态下的样式 */
    static lv_style_t style_pr;
    lv_style_init(&style_pr);  // 初始化按下状态样式
    // // 设置图片重新着色不透明度为30%（变暗效果）
    // lv_style_set_img_recolor_opa(&style_pr, LV_OPA_30);
    // // 设置重新着色为黑色（产生变暗效果）
    // lv_style_set_img_recolor(&style_pr, lv_color_black());
    // 设置按下时宽度增加5像素（放大效果）
    lv_style_set_transform_width(&style_pr, 5);

    // 创建一个固定大小的容器
    lv_obj_t * container = lv_obj_create(home_screen);
    lv_obj_remove_style_all(container);
    lv_obj_set_size(container, 38, 38);
    lv_obj_align(container, LV_ALIGN_TOP_LEFT, 0, 0);

    // 在容器内创建图片并居中
    lv_obj_t * img1 = lv_img_create(container);
    lv_img_set_src(img1, &img_cogwheel_argb);
    lv_img_set_zoom(img1, 64);
    lv_obj_center(img1);

    // 为按钮添加默认样式（应用于所有状态）
    lv_obj_add_style(img1, &style_def, 0);
    // 为按钮添加按下状态样式（仅在按下时应用）
    lv_obj_add_style(img1, &style_pr, LV_STATE_PRESSED);

    // 外边框焦点指示
    lv_obj_set_style_radius(img1, 4, LV_STATE_FOCUSED);  // 圆角
    lv_obj_set_style_outline_width(img1, 2, LV_STATE_FOCUSED);
    lv_obj_set_style_outline_color(img1, lv_color_hex(COLOR_LIME_GREEN), LV_STATE_FOCUSED);
    lv_obj_set_style_outline_pad(img1, 2, LV_STATE_FOCUSED);
    
    lv_group_add_obj(g,img1);

    // 添加点击事件回调 - 关键代码
    lv_obj_add_event_cb(img1, img_click_event_cb, LV_EVENT_CLICKED, NULL);
#endif
    lv_refr_now(NULL);
    /* 创建定时器，每1秒调用add_data函数添加新数据 */
    // lv_timer_create(add_data, 1000, NULL);
    // 设置焦点到第一个按钮
    lv_group_focus_obj(arc);

    lv_scr_load(home_screen);
}

// 创建设置界面函数
static void create_settings_screen(void)
{
    // 创建新的屏幕
    settings_screen = lv_obj_create(NULL);
    
    // 设置屏幕背景色
    // lv_obj_set_style_bg_color(settings_screen, lv_color_hex(0x2D2D2D), 0);
#if 1
    // 添加返回按钮
    lv_obj_t * back_btn = lv_btn_create(settings_screen);
    lv_obj_set_size(back_btn, 80, 40);
    lv_obj_align(back_btn, LV_ALIGN_TOP_LEFT, 10, 10);
    lv_obj_t * back_label = lv_label_create(back_btn);
    lv_label_set_text(back_label, "Back");
    lv_obj_center(back_label);
    // 添加编码器事件支持
    lv_group_add_obj(g_setting, back_btn);
    // 外边框焦点指示
    lv_obj_set_style_radius(back_btn, 4, LV_STATE_FOCUSED);  // 圆角
    lv_obj_set_style_outline_width(back_btn, 2, LV_STATE_FOCUSED);
    lv_obj_set_style_outline_color(back_btn, lv_color_hex(COLOR_LIME_GREEN), LV_STATE_FOCUSED);
    lv_obj_set_style_outline_pad(back_btn, 2, LV_STATE_FOCUSED);

    // 返回按钮点击事件
    lv_obj_add_event_cb(back_btn, back_btn_event_cb, LV_EVENT_SHORT_CLICKED, NULL);
    
    // 添加设置界面标题
    lv_obj_t * title = lv_label_create(settings_screen);
    lv_label_set_text(title, "Settings");
    lv_obj_set_style_text_font(title, &lv_font_montserrat_24, 0);
    lv_obj_set_style_text_color(title, lv_color_white(), 0);
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 10);

#endif

#if 1
    // 创建编码器方向切换开关
    switch_ptrs[0].switch_id = lv_switch_create(settings_screen);
    // 设置开关背景颜色（关闭状态）
    lv_obj_set_style_bg_color(switch_ptrs[0].switch_id, lv_color_hex(COLOR_GRAY), LV_PART_MAIN | LV_STATE_DEFAULT);

    // 设置开关背景颜色（打开状态）
    lv_obj_set_style_bg_color(switch_ptrs[0].switch_id, lv_color_hex(COLOR_BLUE), LV_PART_MAIN | LV_STATE_CHECKED);
    lv_obj_set_size(switch_ptrs[0].switch_id, 80, 40);  // 建议设置合适的大小
    lv_obj_align(switch_ptrs[0].switch_id, LV_ALIGN_LEFT_MID, 10, -30);

    lv_obj_add_state(switch_ptrs[0].switch_id, (*(rt_uint8_t*)flash_dat[1].buf)? LV_STATE_CHECKED:LV_STATE_DEFAULT);

    // 添加编码器事件支持
    lv_group_add_obj(g_setting, switch_ptrs[0].switch_id);

    // 创建标签
    lv_obj_t *switch_encoder_label = lv_label_create(settings_screen);
    lv_label_set_text(switch_encoder_label, "Encoder Dir");  // 修正拼写和格式
    lv_obj_align_to(switch_encoder_label, switch_ptrs[0].switch_id, LV_ALIGN_OUT_RIGHT_BOTTOM, 30, 0);  // 调整位置

    // 可选：添加值改变事件回调
    lv_obj_add_event_cb(switch_ptrs[0].switch_id, encoder_switch_event_cb, LV_EVENT_VALUE_CHANGED, NULL);

    // 可选：添加焦点样式
    lv_obj_set_style_bg_color(switch_ptrs[0].switch_id, lv_color_hex(COLOR_GREEN), LV_STATE_FOCUSED);
    lv_obj_set_style_bg_opa(switch_ptrs[0].switch_id, LV_OPA_50, LV_STATE_FOCUSED);
#endif
    lv_refr_now(NULL);
    // 设置焦点到第一个按钮
    lv_group_focus_obj(switch_ptrs[0].switch_id);
    lv_scr_load(settings_screen);
}

static void create_sleeping_screen(void)
{
    sleeping_screen = lv_obj_create(NULL);

    if (sleeping_screen == NULL) {
        LOG_E("sleeping_screen is null");
        return; // 创建失败直接返回
    }
    // 设置屏幕背景色
    lv_obj_set_style_bg_color(sleeping_screen, lv_color_hex(COLOR_PURPLE), 0);

    lv_obj_t * chinese_label = lv_label_create(sleeping_screen);

    lv_label_set_text(chinese_label, "休眠中...");
    lv_obj_set_style_text_font(chinese_label, &lv_font_montserrat_72, 0);
    lv_obj_set_style_text_align(chinese_label, LV_TEXT_ALIGN_CENTER, 0);
    lv_obj_set_style_text_color(chinese_label, lv_color_hex(COLOR_WHITE), 0);
    lv_obj_center(chinese_label);

    // lv_refr_now(NULL);
    // rt_thread_mdelay(100);
    lv_scr_load(sleeping_screen);
}

// 动态更新函数    
// 红色大温度数字
void update_temp_disp_number(int num) {
    // 检查LVGL对象是否有效
    if (temp_disp_lable == NULL) {
        rt_kprintf("ERROR: temp_disp_lable is NULL!\n");
        return;
    }
    static char buf[12];
    lv_snprintf(buf, sizeof(buf), "%d °C", num);
    lv_label_set_text(temp_disp_lable, buf);
}

/**
 * 风速反馈函数
 */
static void lv_arc_fan_speed_disp_cb(lv_event_t * e)
{
    lv_obj_t * arc = lv_event_get_target(e);
    lv_obj_t * arc_label = lv_event_get_user_data(e);

    char buf[4];
    rt_snprintf(buf, sizeof(buf), "%d", lv_arc_get_value(arc));
    // rt_kprintf("flash_dat[2].buf:%d, value:%d\n", *(rt_uint8_t *)flash_dat[2].buf, lv_arc_get_value(arc));
    lv_label_set_text(arc_label, buf);
}

static void encoder_switch_event_cb(lv_event_t *e)
{
   lv_event_code_t code = lv_event_get_code(e);
   lv_obj_t * obj = lv_event_get_target(e);

   if(code == LV_EVENT_VALUE_CHANGED) {
        *((uint8_t*)flash_dat[1].buf) = lv_obj_has_state(obj, LV_STATE_CHECKED) ? 1 : 0;
        flash_dat[1].dirty_flag = RT_TRUE;
        LOG_D("lv_obj_has_state:%d", lv_obj_has_state(obj, LV_STATE_CHECKED));
   } 
}
// 改进的组切换函数
void lv_switch_group(lv_group_t **new_group)
{
    if(*new_group == NULL) {
        *new_group = lv_group_create();
        lv_group_set_editing(*new_group, true);
    }
    
    // 绑定所有编码器设备到新组
    lv_indev_t *cur_drv = NULL;
    int bound_count = 0;
    
    for(;;) {
        cur_drv = lv_indev_get_next(cur_drv);
        if(!cur_drv) break;
        
        if(cur_drv->driver->type == LV_INDEV_TYPE_ENCODER) {
            lv_indev_set_group(cur_drv, *new_group);
            bound_count++;
        }
    }
    
    // 设置默认组
    lv_group_set_default(*new_group);
    
    // 确保组中有焦点对象
    if(lv_group_get_focused(*new_group) == NULL) {
        lv_group_focus_next(*new_group);
    }
    rt_kprintf("Switched to group: %p, bound %d encoder(s)\n", *new_group, bound_count);
}

static void img_click_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    
    if(code == LV_EVENT_CLICKED) {
        // 切换到设置界面组
        lv_switch_group(&g_setting);
        if(settings_screen == NULL) {
            create_settings_screen();
        }
        lv_scr_load_anim(settings_screen, LV_SCR_LOAD_ANIM_MOVE_LEFT, 300, 0, false);
    }
}

static void back_btn_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    
    if(code == LV_EVENT_SHORT_CLICKED) {
        // 切换回主界面组
        lv_switch_group(&g);

        lv_scr_load_anim(home_screen, LV_SCR_LOAD_ANIM_MOVE_RIGHT, 300, 0, false);
    }
}

void lv_user_gui_init(void)
{
    /* display demo; you may replace with your LVGL application at here */
    // 先创建组
    g = lv_group_create();
    g_setting = lv_group_create();
    
    lv_group_set_editing(g, true);
    lv_group_set_editing(g_setting, true);
    
    rt_kprintf("Created groups: g=%p, g_setting=%p\n", g, g_setting);
    
    // 设置默认组为主界面组
    lv_switch_group(&g);
    // 创建主界面
    create_home_screen();
    // create_settings_screen();
    
    rt_kprintf("GUI initialized, current group: %p with %d objects\n", 
               g, lv_group_get_obj_count(g));
}      

rt_thread_t lv_demo_thread;

void lv_demo_entry(void *param)
{
    static rt_mutex_t dynamic_mutex = RT_NULL;
    /* 创建一个动态互斥量 */
    dynamic_mutex = rt_mutex_create("dmutex", RT_IPC_FLAG_PRIO);
    if (dynamic_mutex == RT_NULL)
    {
        rt_kprintf("create dynamic mutex failed.\n");
    }
    static rt_uint32_t lv_demo_cnt = 0;
    static rt_uint16_t temp_buf[20] = {0};
    while (1)
    {
        temp_buf[lv_demo_cnt] = temp_adc_get_value();

        if (++lv_demo_cnt >= 20) {
            for (rt_uint8_t i=0; i<20; i++) {
                temperature_average += temp_buf[i];
            }
            temperature_average = (rt_uint16_t) (temperature_average/20 * 0.145f);
            // LOG_D("temperature_average:%d", temperature_average);
            if (rt_mutex_take(dynamic_mutex, RT_WAITING_FOREVER) == RT_EOK)
            {
                // update_temp_set_number(temp_adc_get_value());
                // rt_kprintf("Demo thread running:%d\n", temp_adc_get_value());
                add_data();
                // LOG_D("lv_group_get_obj_count(g_setting):%d", lv_group_get_obj_count(g_setting));
                // LOG_D("lv_group_get_obj_count(g):%d", lv_group_get_obj_count(g));
                rt_mutex_release(dynamic_mutex);
            }
            lv_demo_cnt = 0;
        }
        rt_thread_mdelay(10);
    }
    
}

int lv_demo_run(void)
{

   lv_demo_thread = rt_thread_create("demo_thread",
                                        lv_demo_entry,
                                        LV_DEMO_PARAMETER,
                                        LV_DEMO_THREAD_STACK_SIZE,
                                        LV_DEMO_PRIORITY,
                                        20);

    if (lv_demo_thread == RT_NULL)
    {
        LOG_E("lv_demo thread create error");
        return -1;
    }
    rt_thread_startup(lv_demo_thread);
    return 0;
}
INIT_APP_EXPORT(lv_demo_run);